﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using DbSharp.GuiRunner.ViewItems;
using DbSharp.Framework;
using DbSharp.Framework.TestEngine;
using System.Reflection;

namespace DbSharp.GuiRunner.ViewItems
{
    [Obfuscation]
    class PerfCaseViewItem : INotifyPropertyChanged
    {
        #region Private members
        private PerfTestRunner m_CaseRunner;
        private ImageList m_ListStateImage;
        private ImageList m_ListExportedImage;
        private string m_CaseName;
        private CaseTestState m_State;
        private Image m_StateImage;
        private bool m_ExportedState;
        private Image m_ExportedImage;
        private double m_FetchTime;
        private double m_ExecutionTime;
        private double m_TotalTime;

        #endregion

        public PerfCaseViewItem(ImageList listStateImage, ImageList listExportedImage, PerfTestRunner caseRunner)
        {
            m_ListStateImage = listStateImage;
            m_ListExportedImage = listExportedImage;
            m_CaseRunner = caseRunner;
            Refresh();
        }

        #region Public property/method

        public void Refresh()
        {
            this.CaseName = m_CaseRunner.TestCase.TestCaseName;
            this.State = m_CaseRunner.CaseExecuteResult.TestState;
            this.ExportedState = m_CaseRunner.CaseExecuteResult.ExportedFlag;
            if (m_CaseRunner.CaseExecuteResult.ListStatementResult.Count == 0)
                return;

            this.FetchTime = m_CaseRunner.CaseExecuteResult.ListStatementResult[0].FetchDataMilliseconds;
            this.ExecutionTime = m_CaseRunner.CaseExecuteResult.ListStatementResult[0].ExecuteMilliseconds;
            this.TotalTime = m_CaseRunner.CaseExecuteResult.ListStatementResult[0].TotalMilliseconds;
        }

        public PerfTestRunner TestRunner
        {
            get { return m_CaseRunner; }
        }

        public string CaseName
        {
            get
            {
                return m_CaseName;
            }
            private set
            {
                m_CaseName = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("CaseName"));
                }
            }
        }

        public CaseTestState State
        {
            get { return m_State; }
            set
            {
                m_State = value;
                m_StateImage = m_ListStateImage.Images[(int)m_State];
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("StateImage"));
                }
            }
        }

        public Image StateImage
        {
            get { return m_StateImage; }
        }


        public bool ExportedState
        {
            get { return m_ExportedState; }
            private set
            {
                m_ExportedState = value;
                if (m_ExportedState == false)
                    m_ExportedImage = m_ListExportedImage.Images[0];
                else
                    m_ExportedImage = m_ListExportedImage.Images[1];
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("ExportedImage"));
                }
            }
        }


        public Image ExportedImage
        {
            get { return m_ExportedImage; }
        }


        public double FetchTime
        {
            get { return m_FetchTime; }
            private set
            {
                m_FetchTime = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("FetchTime"));
                }
            }
        }

        public double TotalTime
        {
            get { return m_TotalTime; }
            private set
            {
                m_TotalTime = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("TotalTime"));
                }
            }
        }

        public double ExecutionTime
        {
            get { return m_ExecutionTime; }
            private set
            {
                m_ExecutionTime = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("ExecutionTime"));
                }
            }
        }

        string m_ProgressText = string.Empty;
        public string ProgressText
        {
            get { return m_ProgressText; }
            set
            {
                m_ProgressText = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("ProgressText"));
                }
            }
        }

        #endregion

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion
    }
}
